﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GSPatEditor.SoundEffect
{
    class CV3ToWavStream
    {
        public static byte[] ToByteArray(Stream cv3)
        {
            var ret = new byte[cv3.Length + 22];

            //fill header
            int length1 = (int)cv3.Length;
            int length2;
            using (var ms = new MemoryStream(ret))
            {
                using (var bw = new BinaryWriter(ms))
                {
                    using (var br = new BinaryReader(cv3))
                    {
                        bw.Write(new byte[] { 0x52, 0x49, 0x46, 0x46 });
                        bw.Write(length1 + 14);
                        bw.Write(new byte[] { 0x57, 0x41, 0x56, 0x45 });
                        bw.Write(new byte[] { 0x66, 0x6d, 0x74, 0x20 });
                        bw.Write((int)16);
                        bw.Write(br.ReadInt32());
                        bw.Write(br.ReadInt32());
                        bw.Write(br.ReadInt32());
                        bw.Write(br.ReadInt32());
                        bw.Write(new byte[] { 0x64, 0x61, 0x74, 0x61 });
                        br.ReadInt16();
                        length2 = br.ReadInt32();
                        bw.Write(length2);

                        cv3.Read(ret, 0x2c, length2);
                    }
                }
            }
            return ret;
        }
    }
}
